package com.neteast.androidclient.contact.widget;


import com.neteast.androidclient.contact.R;
import com.neteast.androidclient.contact.util.LetterTouchListener;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

public class LetterListView extends View {
	private Context mContext;
	private String[] letters=null;
	private Paint textPaint;
	private LetterTouchListener listener;
	private boolean isShow;
	private int currentIndex;
	private TextView textView;
	private PopupWindow pop;
	private int letterLength;
	private int totalHeight;
	private int totalWidth;
	private int singleTextHeight;
	private int color=0x70ffffff;
	
	public LetterListView(Context context) {
		this(context,null);
	}
	public LetterListView(Context context, AttributeSet attrs) {
		this(context, attrs,0);
	}
	public LetterListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;
		letters=mContext.getResources().getStringArray(R.array.search_index_arrays);
		textPaint=new Paint();
		textPaint.setColor(0xff666666);
		textPaint.setTextSize(14.0f);
		textPaint.setAntiAlias(true);
		textPaint.setTextAlign(Paint.Align.CENTER);
		
		
		setFocusable(true);
        textView = (TextView) LayoutInflater.from(context).inflate(R.layout.toast, null);
        pop = new PopupWindow(textView, 120, 120);
	}
	
	private void setToastLetter(String letter) {
		textView.setText(letter);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		letterLength = letters.length;
		totalHeight = getHeight();
		totalWidth = getWidth();
		singleTextHeight = totalHeight/letterLength;
		textPaint.setTextSize(totalHeight*5/6/letterLength);
		
		Paint paint = new Paint();
		paint.setColor(color);
		canvas.drawRoundRect(new RectF(0, 0, totalWidth, totalHeight), 15, 15, paint);
		
		Paint selectPaint=new Paint();
		selectPaint.setAntiAlias(true);
		selectPaint.setColor(mContext.getResources().getColor(R.color.search_index_focus));
		
		canvas.drawRect(new RectF(0, 
				singleTextHeight*currentIndex+singleTextHeight/5, 
				getWidth(), 
				singleTextHeight*(currentIndex+1)+singleTextHeight/5), 
				selectPaint);
		
		for(int i=0;i<letterLength;i++){
			String letter=letters[i];
			int x=totalWidth/2;
			int y=singleTextHeight*(i+1);
			canvas.drawText(letter, x, y, textPaint);
		}
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			if (!isShow) {
				pop.showAtLocation(this, Gravity.CENTER, 0, 0);
				isShow=true;
				color=mContext.getResources().getColor(R.color.search_index_focus);
				invalidate();
			}
			executeEvent(event);
			break;
		case MotionEvent.ACTION_MOVE:
			executeEvent(event);
			break;
		case MotionEvent.ACTION_UP:
			isShow=false;
			pop.dismiss();
			color=0x70ffffff;
			invalidate();
			break;
		}
		return true;
	}
	
	private void executeEvent(MotionEvent event) {
		int index;
		index = computeIndex(event);
		if (currentIndex!=index) {
			setToastLetter(letters[index]);
			listener.filterByLitter(letters[index]);
			currentIndex=index;
		}
	}
	
	private int computeIndex(MotionEvent event) {
		int index;
		index=(int) (event.getY()/singleTextHeight);
		index=index<0?0:index;
		index=index>26?26:index;
		return index;
	}
	
	public void setOnTouchingLetterChangedListener(LetterTouchListener listener){
	    this.listener=listener;
	}
	
	public void refresh() {
		currentIndex=0;
		invalidate();
	}
}
